<template>
  <list-card>
    <el-form ref="queryForm" slot="search" inline :model="queryParams" size="small">
      <el-form-item label="操作人员" prop="userName">
        <el-input v-model="queryParams.userName" clearable placeholder="请输入用户名称"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item v-if="showSearch" label="系统模块" prop="moduleName">
        <el-input v-model="queryParams.moduleName" clearable placeholder="请输入系统模块"
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item v-if="showSearch" label="操作结果" prop="result">
        <el-select v-model="queryParams.result" clearable placeholder="登录结果">
          <el-option v-for="item in localDict.system_result" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item v-if="showSearch" label="设备类型" prop="deviceType">
        <el-select v-model="queryParams.deviceType" clearable placeholder="设备类型">
          <el-option v-for="item in localDict.device_type" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item v-if="showSearch" label="操作类型" prop="busType">
        <el-select v-model="queryParams.busType" clearable placeholder="操作类型">
          <el-option v-for="item in localDict.operate_type" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item v-if="showSearch" label="操作时间">
        <el-date-picker v-model="queryParams.params.operTime" :default-time="['00:00:00', '23:59:59']" type="daterange"
                        start-placeholder="开始日期" range-separator="-" end-placeholder="结束日期"
                        value-format="yyyy-MM-dd HH:mm:ss" :picker-options="pickerOptions"/>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div slot="operate">
      <el-button v-has-permi="['monitor:operlog:remove']" :disabled="multiple" icon="el-icon-delete" plain size="mini"
                 type="danger" @click="handleDelete">删除
      </el-button>
      <el-button v-has-permi="['monitor:operlog:remove']" icon="el-icon-delete" plain size="mini" type="danger"
                 @click="handleClean">清空
      </el-button>
      <el-button v-has-permi="['monitor:operlog:export']" icon="el-icon-download" plain size="mini" type="warning"
                 @click="handleExport">导出
      </el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </div>
    <el-table slot="content" ref="tables" v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="操作模块" prop="moduleName"/>
      <el-table-column align="center" label="操作类型" prop="busType">
        <template slot-scope="scope">
          <dict-tag :options="localDict.operate_type" :value="scope.row.busType"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="请求方式" prop="reqMethod"/>
      <el-table-column align="center" label="操作人员" prop="userName" width="100"/>
      <el-table-column :show-overflow-tooltip="true" align="center" label="操作地址" prop="clientIp" width="130"/>
      <el-table-column align="center" label="操作结果" prop="result">
        <template slot-scope="scope">
          <dict-tag :options="localDict.system_result" :value="scope.row.result"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作日期" prop="operTime" width="180"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button v-has-permi="['monitor:login:remove']" icon="el-icon-delete" size="mini" type="text"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination slot="page" v-show="pageParam.total > 0" :total="pageParam.total" :limit.sync="pageParam.pageSize"
                :page.sync="pageParam.pageNum" @pagination="getList"/>
  </list-card>
</template>

<script>
import {deleteAction, getStaticData} from "@/api/common";
import {list, remove} from "@/api/base";
import moment from "moment";

export default {
  name: "Operlog",
  components: {},
  dicts: [],
  data() {
    return {
      // 选中数组
      ids: [],
      // 遮罩层
      loading: true,
      baseUrl: '/monitor/operlog/',
      // 非多个禁用
      multiple: true,
      dataList: [],
      pageParam: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
      },
      // 查询参数
      queryParams: {
        params: {
          operTime: undefined,
        }
      },
      localDict: {
        device_type: [],
        operate_type: [],
        system_result: []
      },
      // 显示搜索条件
      showSearch: false,
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
    };
  },
  created() {
    this.getLocalDict();
    this.resetQuery()
  },
  methods: {
    async getLocalDict() {
      await getStaticData('system-device-type').then(res => {
        this.localDict.device_type = res.data
      })
      await getStaticData('system-operate-type').then(res => {
        this.localDict.operate_type = res.data
      })
      await getStaticData('system-result').then(res => {
        this.localDict.system_result = res.data
      })
    },
    /** 查询登录日志 */
    getList() {
      this.loading = true;
      list(this.baseUrl, {
        ...this.queryParams,
        pageNum: this.pageParam.pageNum,
        pageSize: this.pageParam.pageSize
      }).then(response => {
          this.dataList = response.rows;
          this.pageParam.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageParam.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        params: {
          operTime: undefined,
        }
      }
      this.resetForm("queryForm");
      this.handleQuery()
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const infoIds = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的操作记录？').then(() => {
        remove(this.baseUrl, infoIds, infoIds.length > 1).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      });
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$modal.confirm('是否确认清空所有登录日志数据项？').then(() => {
        deleteAction(`${this.baseUrl}/clean`, this.queryParams).then(() => {
          this.getList();
          this.$modal.msgSuccess("清空成功");
        }).catch(() => {
        });
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(`${this.baseUrl}export`, {
        ...this.queryParams
      }, `系统操作日志_${moment().format('YYYY-MM-DD')}.xlsx`)
    }
  }
};
</script>

